package com.dsp.services.impl;

import java.io.Serializable;

import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;

import com.core.util.DspUtil;
import com.core.util.GenericDAO;
import com.core.util.GenericServiceHibernate;
import com.dsp.bean.Employee;
import com.dsp.dao.EmployeeDAO;
import com.dsp.services.EmployeeService;

@Service("employeeService")
public class EmployeeServiceImpl extends GenericServiceHibernate<Employee, Long> implements EmployeeService {

	Logger logger = Logger.getLogger(DspUtil.Log4J);

	private EmployeeDAO employeeDAO;

	@Resource(name = "employeeDAO")
	public void setDao(GenericDAO<Employee, Serializable> dao) {
		super.setDao(dao);
	}

	/**
	 * @return the employeeDAO
	 */
	public EmployeeDAO getEmployeeDAO() {
		return employeeDAO;
	}

	/**
	 * @param employeeDAO
	 *            the employeeDAO to set
	 */
	public void setEmployeeDAO(EmployeeDAO employeeDAO) {
		this.employeeDAO = employeeDAO;
	}

	@Override
	public String save(Employee entry) throws Exception {
		logger.debug("员工资料正在保存.......");
		DetachedCriteria dc = DetachedCriteria.forClass(Employee.class);
		dc.add(Restrictions.eq("pyCode", entry.getPyCode()));
		dc.add(Restrictions.eq("empidn", entry.getEmpidn()));
		dc.add(Restrictions.eq("company", entry.getCompany()));
		if (entry.getId() != null)
			dc.add(Restrictions.ne("id", entry.getId()));
		List<Employee> list = super.likeBy(dc);
		if (DspUtil.isEmptyList(list))
			return super.save(entry);
		throw new Exception("员工资料重复,请检查");
	}
}
